<?php
namespace App\Http\Controllers;

use App\Http\Requests\BasicRequest;
use App\Models\ClientUser;
use App\Models\ClientUserHouse;
use App\Models\ClientUserMessage;
use App\Models\ClientUserMessagesContent;
use App\Models\ClientUserMessagesType;
use App\Models\ClientUserRongcloud;
use App\Models\HouseDistrict;
use App\Models\NeighborCouncilComplaints;
use App\Models\NeighborCouncilPublicity;
use App\Models\NeighborCouncilList;
use App\Models\NeighborHelp;
use App\Models\NeighborInfo;
use App\Models\News;
use App\Models\Service;
use App\Models\ServiceCompanyOrganization;
use App\Models\ServiceTeamDetail;
use App\Models\ServiceType;
use Illuminate\Http\Request;
use Illuminate\View\View;

class WebviewController extends CommonController {
	
	public function about_us(){
		return view('webview.about_us');
	}
	
	public function about_us_manager(){
	    return view('webview.about_us_manager');
	}
	
	/**
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 物业业务列表
	 */
	public function service_type(){
		return view('webview.service_type');
	}
	// 物业介绍
	public function service_about(Request $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>1,'company_id'=>$company_id])->value('id');
		$data = Service::where(['type_id'=>$type_id])->first();
		return view('webview.service_about',['data'=>$data]);
	}
	
	// 服务内容
	public function service_content(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>3,'company_id'=>$company_id])->value('id');
		$data = Service::where(['type_id'=>$type_id])->first();
		return view('webview.service_about',['data'=>$data]);
	}
	// 物业公示
	public function service_publicity(BasicRequest $request){
		$uid = $request->input('uid');
          $village_id = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('village_id');

		$organization_id = HouseDistrict::where(['id'=>$village_id])->value('organization_id');
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>4,'company_id'=>$company_id])->value('id');
		
		$data = Service::where(['type_id'=>$type_id])->get();
		return view('webview.service_publicity',['data'=>$data]);
	}
	
	// 物业公示
	public function service_publicity_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = Service::find($id);
		return view('webview.service_publicity_detail',['data'=>$data]);
	}
	
	// 业委会章程
	public function service_list(BasicRequest $request){
	    $data = NeighborCouncilList::get();
	    return view('webview.service_list',['data'=>$data]);
	}
	
	// 业委会章程详情
	public function service_detail(BasicRequest $request){
	    $id = $request->input('id');
	    $data = NeighborCouncilList::find($id);
	    return view('webview.service_detail',['data'=>$data]);
	}
	
	// 办事流程
	public function service_process(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>5,'company_id'=>$company_id])->value('id');
		
		$data = Service::where(['type_id'=>$type_id])->get();
		return view('webview.service_process',['data'=>$data]);
	}
	public function service_process_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = Service::find($id);
		return view('webview.service_process_detail',['data'=>$data]);
	}
	// 法律法规
	public function service_laws(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>6,'company_id'=>$company_id])->value('id');
		$data = Service::where(['type_id'=>$type_id])->get();
		return view('webview.service_laws',['data'=>$data]);
	}
	public function service_laws_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = Service::find($id);
		return view('webview.service_laws_detail',['data'=>$data]);
	}
	// 社区调查
	public function service_survey(BasicRequest $request){
		return view('webview.service_survey');
	}
	// 百问百答
	public function service_question(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		$company_id = ServiceCompanyOrganization::where(['id'=>$organization_id])->value('company_id');
		$type_id = ServiceType::where(['type'=>7,'company_id'=>$company_id])->value('id');
		
		$data = Service::where(['type_id'=>$type_id])->get();
		return view('webview.service_question',['data'=>$data]);
	}
	// 百问百答
	public function service_question_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = Service::find($id);
		return view('webview.service_question_detail',['data'=>$data]);
	}
	
	
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 邻里圈信息详情
	 */
	public function neighbor_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborHelp::list()->where(['neighbor_help.id'=>$id])->pagesParse()->first();
		return view('webview.neighbor_detail',['data'=>$data]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 生活信息导航信息详情
	 */
	public function neighbor_info_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborInfo::find($id);

       
		return view('webview.neighbor_info_detail',['data'=>$data]);
	}

	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 消息类型
	 */
	public function messages_type(BasicRequest $request){
		$uid = $request->input('uid');
		$user_type = ClientUser::findOrFail($uid)->type;
		if($user_type == 3){
			$user_type = 1;
		}
		if($user_type == 5){
		    $user_type = 2;
        }
		$data = ClientUserMessagesType::where(['type'=>$user_type])->orderby('sort','desc')->get()->each(function($v,$k) use ($uid){
			$v->unview_count = group99(ClientUserMessage::where(['uid'=>$uid,'type_id'=>$v->id,'viewed'=>0])->count());
			$latest = ClientUserMessage::select(['created_at','title','content_id'])->where(['uid'=>$uid,'type_id'=>$v->id])->latest()->first();

			if($latest->content_id>0){
                    $content=ClientUserMessagesContent::where(['id'=>$latest->content_id])->first();
                    $v->preview=$content->title;
               }else {
                    $v->preview = $latest->title;
               }
			$v->preview_time = $latest->created_at;
		});
		return view('webview.messages_type',['data'=>$data]);
	}
	
	// 某一消息类型下的消息列表
	public function messages_list(BasicRequest $request){
		$type_id = $request->input('type_id');
		$uid = $request->input('uid');
		$type_name = ClientUserMessagesType::where(['id'=>$type_id])->value('name');
		$data = ClientUserMessage::where(['type_id'=>$type_id,'uid'=>$uid])->latest()->limit(50)->get()->each(function($v,$k){
		     if($v->content_id>0){
                    $content=ClientUserMessagesContent::where(['id'=>$v->content_id])->first();
                    $v->content=$content->content;
                    $v->title=$content->title;
               }
          });
		// 设置为已读
		ClientUserMessage::where(['type_id'=>$type_id])->update(['viewed'=>1]);
		return view('webview.messages_list',['type_name'=>$type_name,'data'=>$data]);
	}
	
	// 消息详情
	public function messages_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = ClientUserMessage::find($id);
		if($data->content_id>0){
               $content=ClientUserMessagesContent::where(['id'=>$data->content_id])->first();
               $data->content=$content->content;
               $data->title=$content->title;
          }
		$type_name = ClientUserMessagesType::where(['id'=>$data->type_id])->value('name');
		return view('webview.messages_detail',['type_name'=>$type_name,'data'=>$data]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 业主投诉历史
	 */
	public function complaints_history(BasicRequest $request){
		$uid = $request->input('uid');
		$data = NeighborCouncilComplaints::where(['uid'=>$uid,'parent_id'=>0,'admin_uid'=>0])->whereIn('status',[1,2])->latest()->limit(50)->get();
		return view('webview.complaints_history',['data'=>$data]);
	}
	
	// 业主投诉详情
	public function complaints_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborCouncilComplaints::findOrFail($id)->detail();
		$user = ClientUser::find($data->to_uid);
		return view('webview.complaints_detail',['data'=>$data,'user'=>$user]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 融云聊天历史记录，通用接口
	 */
	public function rongcloud_history(BasicRequest $request){
		$uid = $request->input('uid');
		$to_uid = $request->input('to_uid');
          $data = ClientUserRongcloud::
          leftJoin('client_user as to_user','to_user.id','=','from_uid')
              ->select('to_user.realname as to_realname','to_user.avatar as to_avatar','client_user_rongcloud.id','client_user_rongcloud.from_uid','client_user_rongcloud.to_uid','client_user_rongcloud.type','client_user_rongcloud.created_at','client_user_rongcloud.content')
              ->where(['from_uid'=>$uid,'to_uid'=>$to_uid])->orWhere(['from_uid'=>$to_uid,'to_uid'=>$uid])->orderby('client_user_rongcloud.created_at','desc')->Pages()->reverse();

          if($request->isMethod('post')){

               return json_success('成功',$data);
          }else {
               return view('webview.rongcloud_history', ['data' => $data, 'uid' => $uid,'to_uid'=>$to_uid]);
          }
	}
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 新闻详情，每次进来浏览数+1
	 */
	public function news_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = News::find($id);
		News::where(['id'=>$id])->increment('viewed_count');
		return view('webview.news_detail',['data'=>$data]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * 生活缴费中转
	 */
	public function living_expenses(BasicRequest $request){
		$uid = $request->input('uid');
		
        $village_id=ClientUserHouse::where(['uid'=>$uid])->value('village_id');
        if($village_id==1){
            echo '暂不支持生活缴费';
        }else{
            return view('webview.living_expenses',['uid'=>$uid]);
        }
	}
	
	public function download() {
	    return view('webview.download');
	}
	
// 	public function download_submit() {
// 	    $app_path = './download/huicheng.jpg';
// 	    return response()->download($app_path);
// 	}
	
	/**
	 * @param BasicRequest $request
	 * @return View
	 * 业委公示
	 */
	public function service_gongshi(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		
		$data = NeighborCouncilPublicity::where(['type'=>0,'organization_id'=>$organization_id])->get();
		return view('webview.service_gongshi',['data'=>$data]);
	}
	public function service_gongshi_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborCouncilPublicity::find($id);
		return view('webview.service_gongshi_detail',['data'=>$data]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return View
	 * 业委公告
	 */
	public function service_gonggao(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		
		$data = NeighborCouncilPublicity::where(['type'=>1,'organization_id'=>$organization_id])->get();
		return view('webview.service_gonggao',['data'=>$data]);
	}
	public function service_gonggao_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborCouncilPublicity::find($id);
		return view('webview.service_gonggao_detail',['data'=>$data]);
	}
	
	/**
	 * @param BasicRequest $request
	 * @return View
	 * 业委章程
	 */
	public function service_zhangcheng(BasicRequest $request){
		$uid = $request->input('uid');
		$primary_house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->value('house_id');
		$organization_id = HouseDistrict::firstOrNew(['id'=>$primary_house])->detail()->details->first()->organization_id;
		
		$data = NeighborCouncilList::where(['type'=>0,'organization_id'=>$organization_id])->get();
		return view('webview.service_zhangcheng',['data'=>$data]);
	}
	public function service_zhangcheng_detail(BasicRequest $request){
		$id = $request->input('id');
		$data = NeighborCouncilPublicity::find($id);
		return view('webview.service_zhangcheng_detail',['data'=>$data]);
	}
}